package tech.yunland.shop.web;

import com.moyuer.bsf.common.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.yunland.shop.domain.model.Exam;
import tech.yunland.shop.domain.service.ExamDescriptionService;
import tech.yunland.shop.domain.service.ExamService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by v on 17-7-3.
 */

@RestController
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamDescriptionService examDescriptionService;

    /**
     * 查询考试
     *
     * @param desid
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public Pagination<Exam> get(@RequestParam(required = false) String desid,
                                @RequestParam(required = false) Integer page,
                                @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = examService.getSize(desid);
        List<Exam> records = examService.get(desid, startIdx, rows);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }

    @RequestMapping(value = "/getDes", method = RequestMethod.GET)
    public Pagination<List<Map<String, Object>>> getDes(@RequestParam(required = false) String param,
                                @RequestParam(required = false) Integer page,
                                @RequestParam(required = false) Integer rows) {
        Pagination result = new Pagination(page, rows);
        Integer startIdx = result.getStartIdx();
        Integer count = examDescriptionService.getDesSize(param);
        List<Map<String, Object>> records = examDescriptionService.getDes(param, startIdx, rows);
        result.setCount(count);
        result.setRecords(records);
        return result;
    }

    /**
     * 新建考试
     *
     * @param exam
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@RequestBody @Valid Exam exam) {
        examService.save(exam);
    }
}
